using Microsoft.Phone.Controls.Maps.Core;
using System;
using System.Device.Location;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls.Maps
{
	public class AerialMode : MercatorMode
	{
		private const string lowDPIMarketsKey = "LowDPIMarkets";
		private const string aerialUriFromatKey = "AerialUriFormat";
		private const string aerialWithLabelsUriFormatKey = "AerialWithLabelsUriFormat";
		private const string aerialWithLabelsLowDPIUriFormatKey = "AerialWithLabelsLowDPIUriFormat";
		private const string aerialLabelsUriFormatKey = "AerialLabelsUriFormat";
		private const string aerialLabelsLowDPIUriFormatKey = "AerialLabelsLowDPIUriFormat";
		private const string aerialSubdomainsKey = "AerialUriSubdomains";
		private const string aerialWithLabelsSubdomainsKey = "AerialWithLabelsUriSubdomains";
		private const string aerialLabelsSubdomainsKey = "AerialLabelsUriSubdomains";
		private const string aerialCoverageMapKey = "AerialCoverageMap";
		private readonly MapTileLayer baseTileLayer;
		private readonly TfeTileSource baseTileSource;
		private readonly MapLayer contentLayer;
		private readonly CopyrightManager copyright;
		private readonly TimeSpan fadeInTime = new TimeSpan(0, 0, 0, 0, 150);
		private readonly TimeSpan fadeOutTime = new TimeSpan(0, 0, 0, 0, 1000);
		private readonly TimeSpan labelsTimeout = new TimeSpan(0, 0, 0, 0, 1100);
		private readonly MapTileLayer overlayTileLayer;
		private string[] lowDPIMarkets = new string[0];
		private readonly TfeTileSource overlayTileSource;
		private bool addedToMap;
		private string[][] aerialLabelsSubdomains;
		private string aerialLabelsUriFormat;
		private string aerialLabelsLowDPIUriFormat;
		private string[][] aerialSubdomains;
		private string aerialTileUriFormat;
		private string[][] aerialWithLabelsSubdomains;
		private string aerialWithLabelsUriFormat;
		private string aerialWithLabelsLowDPIUriFormat;
		private Uri coverageMapUri;
		private bool fadingLabels;
		private bool labels;
		private Storyboard labelsFadeOut;
		private bool restrictMaxZoomLevel;
		private bool configurationLoaded;
		public bool ShouldRestrictMaxZoomLevel
		{
			get
			{
				return this.restrictMaxZoomLevel;
			}
			set
			{
				this.restrictMaxZoomLevel = value;
				this.SetTfeCoverageMap();
			}
		}
		public bool ShouldDisplayLabels
		{
			get
			{
				return this.labels;
			}
			set
			{
				this.labels = value;
				this.UpdateTileLayers();
			}
		}
		public bool ShouldDisplayFadingLabels
		{
			get
			{
				return this.fadingLabels;
			}
			set
			{
				this.fadingLabels = value;
				this.UpdateTileLayers();
			}
		}
		public int UpSampleLevelDelta
		{
			get
			{
				return this.baseTileLayer.UpSampleLevelDelta;
			}
			set
			{
				this.baseTileLayer.UpSampleLevelDelta = value;
			}
		}
		public override UIElement Content
		{
			get
			{
				return this.contentLayer;
			}
		}
		public override bool IsDownloading
		{
			get
			{
				return (this.baseTileLayer != null && this.baseTileLayer.IsDownloading) || (this.ShouldDisplayLabels && this.overlayTileLayer != null && this.overlayTileLayer.IsDownloading);
			}
		}
		public override bool IsIdle
		{
			get
			{
				return this.configurationLoaded && ((this.baseTileLayer != null && this.baseTileLayer.IsIdle) || (this.ShouldDisplayLabels && this.overlayTileLayer != null && this.overlayTileLayer.IsIdle));
			}
		}
		public AerialMode() : this(false)
		{
		}
		public AerialMode(bool labels)
		{
			this.restrictMaxZoomLevel = false;
			this.labels = labels;
			this.baseTileLayer = new MapTileLayer(this);
			this.baseTileLayer.UpSampleLevelDelta = 7;
			this.baseTileLayer.set_IsHitTestVisible(true);
			this.baseTileSource = new TfeTileSource();
			this.baseTileLayer.TileSources.Add(this.baseTileSource);
			this.overlayTileLayer = new MapTileLayer(this);
			this.overlayTileLayer.UpSampleLevelDelta = 0;
			this.overlayTileLayer.set_IsHitTestVisible(true);
			this.overlayTileSource = new TfeTileSource();
			this.overlayTileLayer.TileSources.Add(this.overlayTileSource);
			this.contentLayer = new MapLayer();
			this.contentLayer.get_Children().Add(this.baseTileLayer);
			this.UpdateTileLayers();
			this.copyright = CopyrightManager.GetInstance();
			MapConfiguration.GetSection("v1", "WP7SLMapControl", null, new MapConfigurationCallback(this.AsynchronousConfigurationLoaded));
		}
		protected override Range<double> GetZoomRange(GeoCoordinate center)
		{
			return base.GetZoomRange(this.baseTileSource, center);
		}
		protected override void OnCultureChanged()
		{
			if (this.addedToMap)
			{
				this.SetupMode();
			}
		}
		internal override void UpdateAttribution()
		{
			MapStyle style = this.ShouldDisplayLabels ? MapStyle.AerialWithLabels : MapStyle.Aerial;
			this.copyright.RequestCopyrightString(style, this.Center, this.ZoomLevel, this.CredentialsProvider, base.Culture, new Action<CopyrightResult>(this.CopyrightCallback));
		}
		private void CopyrightCallback(CopyrightResult result)
		{
			if (result.Culture == base.Culture && result.Location == this.Center && result.ZoomLevel == this.ZoomLevel)
			{
				base.UpdateAttribution(result.CopyrightStrings);
			}
		}
		public override void Activating(MapMode previousMode, MapLayerBase modeLayer, MapLayerBase modeForegroundLayer)
		{
			base.Activating(previousMode, modeLayer, modeForegroundLayer);
			this.SetupMode();
			this.addedToMap = true;
		}
		private void AsynchronousConfigurationLoaded(MapConfigurationSection config, object userState)
		{
			if (config != null)
			{
				string text = config["LowDPIMarkets"];
				if (!string.IsNullOrEmpty(text))
				{
					this.lowDPIMarkets = text.Split(new char[]
					{
						';'
					});
				}
				this.aerialTileUriFormat = config["AerialUriFormat"];
				this.aerialLabelsUriFormat = config["AerialLabelsUriFormat"];
				this.aerialLabelsLowDPIUriFormat = config["AerialLabelsLowDPIUriFormat"];
				this.aerialWithLabelsUriFormat = config["AerialWithLabelsUriFormat"];
				this.aerialWithLabelsLowDPIUriFormat = config["AerialWithLabelsLowDPIUriFormat"];
				string[][] array;
				this.aerialSubdomains = ((!config.Contains("AerialUriSubdomains") || !MercatorMode.TryParseSubdomains(config["AerialUriSubdomains"], out array)) ? null : array);
				this.aerialWithLabelsSubdomains = ((!config.Contains("AerialWithLabelsUriSubdomains") || !MercatorMode.TryParseSubdomains(config["AerialWithLabelsUriSubdomains"], out array)) ? null : array);
				this.aerialLabelsSubdomains = ((!config.Contains("AerialLabelsUriSubdomains") || !MercatorMode.TryParseSubdomains(config["AerialLabelsUriSubdomains"], out array)) ? null : array);
				this.UpdateTileLayers();
				if (config.Contains("AerialCoverageMap"))
				{
					this.coverageMapUri = new Uri(config["AerialCoverageMap"].Replace("{UriScheme}", "HTTP"), 1);
					if (this.addedToMap)
					{
						this.SetupMode();
					}
				}
			}
			this.configurationLoaded = true;
		}
		private void SetupMode()
		{
			this.UpdateTileLayers();
			base.UpdateAttribution(ProjectionUpdateLevel.None);
			this.SetTfeCoverageMap();
		}
		private void SetTfeCoverageMap()
		{
			this.baseTileSource.SetCoverageMapUri(this.restrictMaxZoomLevel ? this.coverageMapUri : null);
		}
		private void UpdateTileLayers()
		{
			if (!this.ShouldDisplayLabels)
			{
				this.SetUriFormat(this.baseTileSource, this.aerialTileUriFormat, this.aerialSubdomains);
				this.RemoveOverlayTileLayer();
				this.DisableFadingLabels();
				return;
			}
			bool flag = false;
			if (base.Culture != null)
			{
				string text = base.Culture.get_Name().ToLowerInvariant();
				string[] array = this.lowDPIMarkets;
				for (int i = 0; i < array.Length; i++)
				{
					string text2 = array[i];
					if (!string.IsNullOrEmpty(text2) && text2.ToLowerInvariant() == text)
					{
						flag = true;
						break;
					}
				}
			}
			if (this.ShouldDisplayFadingLabels)
			{
				string uriFormat = (!flag || string.IsNullOrEmpty(this.aerialLabelsLowDPIUriFormat)) ? this.aerialLabelsUriFormat : this.aerialLabelsLowDPIUriFormat;
				this.SetUriFormat(this.baseTileSource, this.aerialTileUriFormat, this.aerialSubdomains);
				this.AddOverlayTileLayer();
				this.SetUriFormat(this.overlayTileSource, uriFormat, this.aerialLabelsSubdomains);
				this.EnableFadingLabels();
				return;
			}
			string uriFormat2 = (!flag || string.IsNullOrEmpty(this.aerialWithLabelsLowDPIUriFormat)) ? this.aerialWithLabelsUriFormat : this.aerialWithLabelsLowDPIUriFormat;
			this.SetUriFormat(this.baseTileSource, uriFormat2, this.aerialWithLabelsSubdomains);
			this.RemoveOverlayTileLayer();
			this.DisableFadingLabels();
		}
		private void SetUriFormat(TfeTileSource tileSource, string uriFormat, string[][] subdomains)
		{
			if (!string.IsNullOrEmpty(uriFormat))
			{
				tileSource.UriFormat = uriFormat.Replace("{Culture}", base.Culture.get_Name());
				tileSource.SetSubdomains(subdomains);
			}
		}
		private void AddOverlayTileLayer()
		{
			if (!this.contentLayer.get_Children().Contains(this.overlayTileLayer))
			{
				this.contentLayer.get_Children().Add(this.overlayTileLayer);
			}
		}
		private void RemoveOverlayTileLayer()
		{
			if (this.contentLayer.get_Children().Contains(this.overlayTileLayer))
			{
				this.contentLayer.get_Children().Remove(this.overlayTileLayer);
			}
		}
		private void EnableFadingLabels()
		{
			if (this.labelsFadeOut == null)
			{
				this.labelsFadeOut = new Storyboard();
				DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
				PresentationFrameworkCollection<DoubleKeyFrame> arg_49_0 = doubleAnimationUsingKeyFrames.get_KeyFrames();
				LinearDoubleKeyFrame linearDoubleKeyFrame = new LinearDoubleKeyFrame();
				linearDoubleKeyFrame.set_Value(1.0);
				linearDoubleKeyFrame.set_KeyTime(this.fadeInTime);
				arg_49_0.Add(linearDoubleKeyFrame);
				PresentationFrameworkCollection<DoubleKeyFrame> arg_86_0 = doubleAnimationUsingKeyFrames.get_KeyFrames();
				LinearDoubleKeyFrame linearDoubleKeyFrame2 = new LinearDoubleKeyFrame();
				linearDoubleKeyFrame2.set_Value(1.0);
				linearDoubleKeyFrame2.set_KeyTime(this.fadeInTime + this.labelsTimeout);
				arg_86_0.Add(linearDoubleKeyFrame2);
				PresentationFrameworkCollection<DoubleKeyFrame> arg_CE_0 = doubleAnimationUsingKeyFrames.get_KeyFrames();
				LinearDoubleKeyFrame linearDoubleKeyFrame3 = new LinearDoubleKeyFrame();
				linearDoubleKeyFrame3.set_Value(0.0);
				linearDoubleKeyFrame3.set_KeyTime(this.fadeInTime + this.labelsTimeout + this.fadeOutTime);
				arg_CE_0.Add(linearDoubleKeyFrame3);
				Storyboard.SetTarget(doubleAnimationUsingKeyFrames, this.overlayTileLayer);
				Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath(UIElement.OpacityProperty));
				this.labelsFadeOut.get_Children().Add(doubleAnimationUsingKeyFrames);
			}
			base.ProjectionChanged += new EventHandler<ProjectionChangedEventArgs>(this.MapMode_ProjectionChangedForFadingLabels);
			this.contentLayer.add_MouseMove(new MouseEventHandler(this.ContentLayer_MouseMoveForFadingLabels));
			this.overlayTileLayer.set_Opacity(1.0);
		}
		private void DisableFadingLabels()
		{
			base.ProjectionChanged -= new EventHandler<ProjectionChangedEventArgs>(this.MapMode_ProjectionChangedForFadingLabels);
			this.contentLayer.remove_MouseMove(new MouseEventHandler(this.ContentLayer_MouseMoveForFadingLabels));
			if (this.labelsFadeOut != null)
			{
				this.labelsFadeOut.Stop();
			}
			this.overlayTileLayer.set_Opacity(1.0);
		}
		private void MapMode_ProjectionChangedForFadingLabels(object sender, ProjectionChangedEventArgs e)
		{
			this.ShowLabels();
		}
		private void ContentLayer_MouseMoveForFadingLabels(object sender, MouseEventArgs e)
		{
			this.ShowLabels();
		}
		private void ShowLabels()
		{
			if (this.labelsFadeOut != null)
			{
				this.labelsFadeOut.Begin();
			}
			else
			{
				this.overlayTileLayer.set_Opacity(1.0);
			}
			if (!this.overlayTileLayer.IsIdle)
			{
				this.overlayTileLayer.get_Dispatcher().BeginInvoke(new Action(this.ShowLabels));
			}
		}
	}
}
